using Grpc.Core;
using GrpcService1.Services;
using Microsoft.IdentityModel.Tokens;
using DemoClient = GrpcDemo.Protos.Demo.DemoClient;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpContextAccessor();
builder.Services.AddGrpcReflection();
builder.Services.AddGrpc(o =>
{
    o.Interceptors.Add<ParamInterceptor>();
});
builder.Services.AddGrpcClient<DemoClient>(o =>
{
    o.Address = new Uri("http://localhost:5210");
}).ConfigureChannel(x =>
{
	x.Credentials = ChannelCredentials.SecureSsl;
});
builder.Services.AddAuthentication()
    .AddJwtBearer(o =>
    {
        o.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = false,
            ValidateAudience = false,
            ValidateLifetime = false,
            ValidateIssuerSigningKey = false,
            IssuerSigningKey = GlobalConst.RsaPublicSecurityKey
        };
    });
builder.Services.AddAuthorization();

var app = builder.Build();
app.UseAuthentication(); //经过鉴权中间件，将jwt token转换为claims
app.UseAuthorization();
app.MapGrpcService<DemoService>();
if (app.Environment.IsDevelopment())
{
    app.MapGrpcReflectionService().AllowAnonymous(); //启用反射，将grpc服务暴露出来（无需通过proto生成客户端也能调用），允许匿名访问
}
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();
